package com.winhxd.b2c.customer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.customer.condition.CustomerStoreInfoCondition;
import com.winhxd.b2c.common.domain.customer.condition.ShopPlanCondition;
import com.winhxd.b2c.common.domain.customer.model.CustomerUserInfo;
import com.winhxd.b2c.common.domain.customer.vo.CustomerStoreVO;
import com.winhxd.b2c.common.domain.customer.vo.ShopPlanVO;
import com.winhxd.b2c.common.domain.system.login.condition.CustomerUserInfoCondition;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.customer.dao.CustomerUserInfoMapper;
import com.winhxd.b2c.customer.service.CustomerStoreInfoSerivce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author: zhaoyanbiao
 * @Date: 2019/1/11 16：25
 * @Description: 用户门店信息业务处理层
 */
@Service
public class CustomerStoreInfoServiceImpl implements CustomerStoreInfoSerivce {

    private static final Logger logger = LoggerFactory.getLogger(CustomerStoreInfoServiceImpl.class);

    @Autowired
    private CustomerUserInfoMapper customerUserInfoMapper;


    /**
     * 查询用户门店列表
     * @param condition
     * @return
     */
    @Override
    public PagedList<CustomerStoreVO> findCustomerStoreList(CustomerStoreInfoCondition condition) {
        PagedList<CustomerStoreVO> pagedList = new PagedList<>();
        PageHelper.startPage(condition.getPageNo(),condition.getPageSize());
        List<CustomerStoreVO> storeInfoVOS = customerUserInfoMapper.findCustomerStoreList(condition);
        PageInfo pageInfo = new PageInfo(storeInfoVOS);
        pagedList.setData(storeInfoVOS);
        pagedList.setPageNo(condition.getPageNo());
        pagedList.setPageSize(condition.getPageSize());
        pagedList.setTotalRows(pageInfo.getTotal());
        return pagedList;
    }

    /**
     * 修改门店信息
     * @param condition
     * @return
     */
    @Override
    public Boolean modifyCustomerStoreInfo(CustomerStoreInfoCondition condition) {
        if(condition.getId() ==null )
        {
            logger.error("修改门店信息入参为空--入参为--{}",JsonUtil.toJSONString(condition));
            throw new BusinessException(BusinessCode.CODE_611111);
        }
        CustomerUserInfo storeUserInfo = customerUserInfoMapper.selectByPrimaryKey(condition.getId());
        if(null == storeUserInfo)
        {
            logger.error("根据主键ID查询门店用户信息不存在--入参为--{}",JsonUtil.toJSONString(condition));
            throw new BusinessException(BusinessCode.CODE_611112);
        }
        storeUserInfo.setId(condition.getId());
        if(condition.getStoreStatus() != null)
        {
            storeUserInfo.setStoreStatus(condition.getStoreStatus()==1?Short.parseShort("0"):Short.parseShort("1"));
        }
        storeUserInfo.setStoreName(condition.getStoreName());
        storeUserInfo.setShopLogo(condition.getShopLogo());
        storeUserInfo.setStoreExplain(condition.getStoreExplain());
        storeUserInfo.setUpdated(new Date());
        storeUserInfo.setUpdatedBy(condition.getUpdatedBy());
        storeUserInfo.setUpdatedByName(condition.getUpdatedByName());
        return customerUserInfoMapper.updateByPrimaryKeySelective(storeUserInfo)>=1?true:false;
    }

    @Override
    public CustomerUserInfo getCustomerUserById(Long id) {
        return customerUserInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public PagedList<ShopPlanVO> findCustomerShopPlanList(ShopPlanCondition condition) {
        PagedList<ShopPlanVO> pagedList = new PagedList<>();
        PageHelper.startPage(condition.getPageNo(),condition.getPageSize());
        List<ShopPlanVO> shopPlanVOList = new ArrayList<ShopPlanVO>();
        ShopPlanVO vo=new ShopPlanVO();
        vo.setId(1L);
        vo.setAuditMode(1L);
        vo.setPlanName("代理");
        vo.setCreated("2018-12-12 13:30:45");
        shopPlanVOList.add(vo);
        PageInfo pageInfo = new PageInfo(shopPlanVOList);
        pagedList.setData(pageInfo.getList());
        pagedList.setPageNo(condition.getPageNo());
        pagedList.setPageSize(condition.getPageSize());
        pagedList.setTotalRows(pageInfo.getTotal());
        return pagedList;
    }

    /**
     * 返回当前用户的上级信息
     * @param customerId
     * @return
     */
    @Override
    public CustomerUserInfo getParentCustomerUserByCustoemrId(Long customerId) {
        CustomerUserInfo customerUserInfo = customerUserInfoMapper.selectByPrimaryKey(customerId);
        if(customerUserInfo.getLevel() > 0)
        {
            return  customerUserInfo;
        }else{
           return  customerUserInfoMapper.selectByPrimaryKey(customerUserInfo.getParentId());
        }
    }
}
